package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.common.context.LoginUserContext;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.LeaseStatus;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.BrowsingHistoryService;
import com.atguigu.lease.web.app.service.RoomInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.attr.AttrValueVo;
import com.atguigu.lease.web.app.vo.fee.FeeValueVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.atguigu.lease.web.app.vo.room.RoomDetailVo;
import com.atguigu.lease.web.app.vo.room.RoomItemVo;
import com.atguigu.lease.web.app.vo.room.RoomQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
@Slf4j
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private RoomFacilityMapper roomFacilityMapper;
    @Autowired
    private RoomPaymentTypeMapper roomPaymentTypeMapper;
    @Autowired
    private RoomAttrValueMapper roomAttrValueMapper;
    @Autowired
    private RoomLeaseTermMapper roomLeaseTermMapper;
    @Autowired
    private RoomLabelMapper roomLabelMapper;
    @Autowired
    private BrowsingHistoryService browsingHistoryService;


    @Override
    public IPage<RoomItemVo> findRoomItemVoPage(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.findRoomItemVoPage(page, queryVo);
    }

    @Override
    public RoomDetailVo findDetailById(Long id) {
        //查询房间信息
        RoomInfo roomInfo = this.getById(id);
        //获取房间对应的图片信息
        List<GraphVo> graphVoList = graphInfoMapper.findGraphVoListByItemTypeAndItemId(ItemType.ROOM,id);
        //获取房间对应的配套信息
        List<FacilityInfo> roomFacilitieList = roomFacilityMapper.findRoomFacilitieListById(id);
        //获取房间对应的标签信息
        List<LabelInfo> roomLabelList = roomLabelMapper.findRoomLabelListById(id);
        //获取房间对应的支付方式
        List<PaymentType> roomPaymentTypeList = roomPaymentTypeMapper.findRoomPaymentTypeListById(id);
        //获取房间对应的属性信息
        List<AttrValueVo> roomAttrValueList = roomAttrValueMapper.findRoomAttrValueList(id);
        //获取房间对应的租期信息
        List<LeaseTerm> roomLeaseTermList = roomLeaseTermMapper.findRoomLeaseTermList(id);
        //创建RoomDetailVo对象
        RoomDetailVo roomDetailVo = new RoomDetailVo();
        //复制
        BeanUtils.copyProperties(roomInfo, roomDetailVo);
        //赋值
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setFacilityInfoList(roomFacilitieList);
        roomDetailVo.setLabelInfoList(roomLabelList);
        roomDetailVo.setPaymentTypeList(roomPaymentTypeList);
        roomDetailVo.setAttrValueVoList(roomAttrValueList);
        roomDetailVo.setLeaseTermList(roomLeaseTermList);

        browsingHistoryService.saveHistory(LoginUserContext.getLoginUser().getUserId(), id);

        return roomDetailVo;
    }

    @Override
    public IPage<RoomItemVo> findRoomItemVoPageByApartmenyId(IPage<RoomItemVo> page, Long id) {
        return roomInfoMapper.findRoomItemVoPageByApartmenyId(page,id);
    }
}




